package com.jeasonzhao.report.engine.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;

import com.jeasonzhao.report.engine.ReportUserInfo;
import com.jeasonzhao.report.engine.irp.IUserProvider;
import com.jeasonzhao.report.model.Report;

public abstract class AbstractSessionUserProvider implements IUserProvider
{
    private String sessionKey = "SystemUserSessionKey";
    public AbstractSessionUserProvider(String userSessionKey)
    {
        this.sessionKey = userSessionKey;
    }

    public String getSessionKey()
    {
        return this.sessionKey;
    }

    private HttpSession getSession(Object renderParameter)
    {
        HttpSession session = null;
        if(renderParameter instanceof PageContext)
        {
            session = ((PageContext) renderParameter).getSession();
        }
        else if(renderParameter instanceof HttpServletRequest)
        {
            session = ((HttpServletRequest) renderParameter).getSession();
        }
        return session;
    }

    public ReportUserInfo getCurrentUser(Object renderParameter)
    {
        HttpSession session = getSession(renderParameter);
        if(null == session)
        {
            return null;
        }
        else
        {
            Object obj = session.getAttribute(sessionKey);
            if(null == obj)
            {
                return null;
            }
            if(obj instanceof ReportUserInfo)
            {
                return(ReportUserInfo) obj;
            }
            else
            {
                ReportUserInfo user = new ReportUserInfo();
                //SimpleBeanWrapper wrapper = SimpleBeanFactory.getInstance().getWrapper(obj.getClass());
//                user.setId(Algorithms.toString(getBeanValue(wrapper,obj,"userid","id","guid","primarykey")));
//                user.setAdministrator(ConvertEx.toBool(getBeanValue(wrapper,obj,"administrator","isAdministrator"
//                    ,"administration","isadministration","admin","isadmin","reportadmin","isreportadmin")));
//                user.setName(Algorithms.toString(getBeanValue(wrapper,obj,
//                    "username","name","title")));
                this.fillUserProperties(user,obj);
                return user;
            }
        }
    }

//    private Object getBeanValue(SimpleBeanWrapper wrapper,Object obj,String ...names)
//    {
//        try
//        {
//            if(null == wrapper || null == obj || null == names)
//            {
//                return null;
//            }
//            else
//            {
//                return wrapper.getOneOfValue(obj,names);
//            }
//        }
//        catch(IllegalAccessException ex)
//        {
//            return null;
//        }
//        catch(IllegalArgumentException ex)
//        {
//            return null;
//        }
//        catch(InvocationTargetException ex)
//        {
//            return null;
//        }
//    }

    protected abstract void fillUserProperties(ReportUserInfo user,Object sessionObject);

    public void onAddReport(ReportUserInfo user,Report report)
    {
    }

    public void onDeleteReport(ReportUserInfo user,Report report)
    {
    }

    public boolean isAccessable(ReportUserInfo user,Report report)
    {
        return false;
    }

}
